package com.wei.tang.db;

import java.util.ArrayList;
import java.util.List;

import com.wei.tang.bean.UserBean;
import com.wei.tang.db.DBInfo.UserTable;
import com.wei.tang.util.Tlog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.test.AndroidTestCase;

/**
 * 
 * @author 吴佳明
 *
 */
public class UserDao extends AndroidTestCase {

	private DBHelper dbHelper = null;
	private SQLiteDatabase db = null;
	private ContentValues values = null;
	String[] columns = { DBInfo.UserTable.USER_NAME };

	public UserDao(Context context) {
		Tlog.i("进入数据库类");
		dbHelper = new DBHelper(context);
		Tlog.i("数据库  表创建成功");
	}

	/**
	 * 用户注册
	 * 
	 * @param userBean
	 * @return
	 */
	public UserBean userRegist(String _userPhone, String _userpass, String _userTag) {
		Tlog.i("用户注册");
		UserBean userBean = null;
		// judge user regist yes return null
		if (findUserByPhone(_userPhone) != null) {
			return userBean;
		}
		userBean = new UserBean(_userPhone, _userpass, _userTag);
		if (insertUser(userBean)) {
		} else {
			userBean = null;
		}
		return userBean;
	}

	/**
	 * 向用户表中插入单条数据，成功返回true 失败返回false
	 * 
	 * @param userBean
	 * @return
	 */
	public boolean insertUser(UserBean userBean) {
		Tlog.i("插入用户数据到数据库中");
		db = dbHelper.getReadableDatabase();
		values = fillContentValues(userBean);
		long rowId = db.insert(DBInfo.UserTable.USER_TABLE, DBInfo.UserTable.USER_NAME, values);
		db.close();
		Tlog.i("是否存成功：" + rowId);
		if (rowId == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 修改用户登录密码
	 * 
	 * @param _userPhone
	 * @param _userPassword
	 * @param _userTag
	 * @return resultCode 100:用户不存在，请前往注册 200:标示错误重新输入 300：修改失败 400:修改成功
	 */
	public int changePassword(String _userPhone, String _userPassword, String _userTag) {
		Tlog.i("用户注册");
		int resultCode;
		UserBean userBean = null;
		// judge user regist yes return null
		userBean = findUserByPhone(_userPhone);
		if (userBean == null) {
			resultCode = 100;
			return resultCode;
		}
		if (!userBean.getUser_tag().equals(_userTag)) {
			resultCode = 200;
			return resultCode;
		}
		userBean.setUser_password(_userPassword);
		if (updateUserInfo(userBean) != 0) {
			resultCode = 400;
		} else {
			resultCode = 300;
		}
		return resultCode;
	}

	/**
	 * 更新用户数据
	 * 
	 * @param userBean
	 * @return
	 */
	public int updateUserInfo(UserBean userBean) {
		Tlog.i("更新用户信息");
		db = dbHelper.getReadableDatabase();
		values = fillContentValues(userBean);
		int rowId = db.update(DBInfo.UserTable.USER_TABLE, values, "user_id=?", new String[] { userBean.getUser_id() });
		return rowId;
	}

	/**
	 * 通过user_name查找用户 查不到会抛空指针异常
	 * 
	 * @param user_id
	 * @return
	 */
	public UserBean findUserByUserName(String user_name) {
		String sql = "user_name=? ";
		String[] strs = { user_name };
		return findUserBySql(sql, strs);
	}

	/**
	 * 通过user_session查找用户
	 * 
	 * @param user_session
	 * @return
	 */
	public UserBean findUserByUserSession(String user_session) {
		String sql = "user_session=?";
		String[] strs = { user_session };
		return findUserBySql(sql, strs);
	}

	/**
	 * 通过user_phone,user_password查找用户
	 * 
	 * @param
	 * @return
	 */
	public UserBean findUserByPhonePass(String user_phone, String user_password) {
		Tlog.i("findUserByPhonePass---user_phone:" + user_phone + "---user_password:" + user_password);
		String sql = "user_phone=? and user_password=?";
		String[] strs = { user_phone, user_password };
		return findUserBySql(sql, strs);
	}

	/**
	 * 通过sql语句查找用户单个用户
	 * 
	 * @param sql
	 * @param strs
	 * @return
	 */
	public UserBean findUserBySql(String sql, String[] strs) {
		Tlog.i("findUserBySql---sql:" + sql);
		db = dbHelper.getReadableDatabase();
		Cursor cur = null;
		UserBean userBean = null;
		try {
			cur = db.query("userinfo", null, sql, strs, null, null, null, null);
			if (cur.moveToNext()) {
				userBean = fillUserBean(cur);
				Tlog.i("findUserBySql---sql:" + userBean);
			}
			cur.close();
		} catch (Exception e) {
			Tlog.e("e:" + e.getMessage());
		}
		db.close();
		return userBean;
	}

	/**
	 * 通过user_phone,user_tag查找用户
	 * 
	 * @param
	 * @return
	 */
	public UserBean findUserByPhoneTag(String user_phone, String user_tag) {
		String sql = "SELECT ＊ FROM userinfo WHERE user_phone=? and user_tag=?";
		String[] strs = { user_phone, user_tag };
		return findUserBySql(sql, strs);
	}

	/**
	 * 通过user_phone查找用户
	 * 
	 * @param
	 * @return
	 */
	public UserBean findUserByPhone(String user_phone) {
		String sql = "user_phone=? ";
		String[] strs = { user_phone };
		return findUserBySql(sql, strs);
	}

	/**
	 * 通过user_id查找用户
	 * 
	 * @param user_id
	 * @return
	 */
	public UserBean findUserByUserId(String user_id) {
		String sql = "SELECT ＊ FROM userinfo WHERE user_id=?";
		String[] strs = { user_id };
		return findUserBySql(sql, strs);
	}

	/**
	 * 查找年纪为user_age的用户
	 * 
	 * @param user_age
	 * @return
	 */
	public List<UserBean> findUsersByUserage(String user_age) {
		String sql = "SELECT * FROM userinfo WHERE user_age=?;";
		String[] strs = { user_age };
		return findUsersBySql(sql, strs);
	}

	/**
	 * 查找名字中含有含有like_name 的用户
	 * 
	 * @param user_id
	 * @return
	 */
	public List<UserBean> findUserByLikeName(String like_name) {
		String sql = "user_name=? ";
		String[] strs = { like_name };
		return findUsersBySql(sql, strs);
	}

	/**
	 * 查找名字中含有含有like_name 的用户
	 * 
	 * @param user_id
	 * @return
	 */
	public List<UserBean> findStuentsByName(String like_name) {
		Tlog.i("findStuentsByName---sql:" + like_name);
		String sql = "user_location=? ";
		String[] strs = { like_name };
		return findUsersBySql(sql, strs);
	}

	/**
	 * 通过sql语句获取多个用户
	 * 
	 * @param sql
	 * @param strs
	 * @return
	 */
	@SuppressWarnings("null")
	public List<UserBean> findUsersBySql(String sql, String[] strs) {
		Tlog.i("findUserBySql---sql:" + sql);
		db = dbHelper.getReadableDatabase();
		Cursor cur = null;
		List<UserBean> userBeans = new ArrayList<UserBean>();
		try {
			cur = db.query("userinfo", null, sql, strs, null, null, null, null);
			if (cur.moveToNext()) {
				UserBean userBean = fillUserBean(cur);
				userBeans.add(userBean);
			}
			cur.close();
		} catch (Exception e) {
			Tlog.e("e:" + e.getMessage());
		}
		db.close();
		return userBeans;
	}

	/**
	 * Bundle数据的注入到UserBean
	 * 
	 * @param bundle
	 * @return
	 */
	private UserBean fillUserBean(Cursor cur) {
		UserBean userBean = new UserBean();
		userBean.setUser_id(cur.getString(cur.getColumnIndex(UserTable.USER_ID)));
		userBean.setUser_password(cur.getString(cur.getColumnIndex(UserTable.USER_PASSWORD)));
		userBean.setUser_phone(cur.getString(cur.getColumnIndex(UserTable.USER_PHONE)));
		userBean.setUser_name(cur.getString(cur.getColumnIndex(UserTable.USER_NAME)));
		userBean.setUser_sex(cur.getString(cur.getColumnIndex(UserTable.USER_SEX)));
		userBean.setUser_session(cur.getString(cur.getColumnIndex(UserTable.USER_SESSION)));
		userBean.setUser_tag(cur.getString(cur.getColumnIndex(UserTable.USER_TAG)));
		userBean.setUser_status(cur.getString(cur.getColumnIndex(UserTable.USER_STATUS)));
		userBean.setDecription(cur.getString(cur.getColumnIndex(UserTable.DECRIPTION)));
		userBean.setUser_time(cur.getString(cur.getColumnIndex(UserTable.USER_TIME)));
		userBean.setUser_head(cur.getString(cur.getColumnIndex(UserTable.USER_HEAD)));
		userBean.setUser_location(cur.getString(cur.getColumnIndex(UserTable.USER_LOCATION)));
		return userBean;
	}

	/**
	 * UserBean数据的注入到ContentValues
	 * 
	 * @param _userBean
	 * @return
	 */
	private ContentValues fillContentValues(UserBean _userBean) {
		ContentValues tempValues = new ContentValues();
		tempValues.put(DBInfo.UserTable.USER_ID, _userBean.getUser_id());
		tempValues.put(DBInfo.UserTable.USER_PHONE, _userBean.getUser_phone());
		tempValues.put(DBInfo.UserTable.USER_PASSWORD, _userBean.getUser_password());
		tempValues.put(DBInfo.UserTable.USER_NAME, _userBean.getUser_name());
		tempValues.put(DBInfo.UserTable.USER_SEX, _userBean.getUser_sex());
		tempValues.put(DBInfo.UserTable.USER_SESSION, _userBean.getUser_session());
		tempValues.put(DBInfo.UserTable.USER_STATUS, _userBean.getUser_status());
		tempValues.put(DBInfo.UserTable.DECRIPTION, _userBean.getDecription());
		tempValues.put(DBInfo.UserTable.USER_LOCATION, _userBean.getUser_location());
		tempValues.put(DBInfo.UserTable.USER_TIME, _userBean.getUser_time());
		tempValues.put(DBInfo.UserTable.USER_TAG, _userBean.getUser_tag());
		tempValues.put(DBInfo.UserTable.USER_HEAD, _userBean.getUser_head());
		return tempValues;
	}

	public String[] findAllUsers() {
		db = dbHelper.getReadableDatabase();
		String usernamelast[] = null;
		String sql = "select c.user_name from userinfo c where 5 > (select count(*) from userinfo where  user_time > c.user_time ) order by c.user_time ";
		Cursor cursor = db.rawQuery(sql, null);
		int i = 0;
		if (cursor != null && cursor.getCount() > 0) {
			usernamelast = new String[cursor.getColumnCount()];
			while (cursor.moveToNext()) {
				usernamelast[i++] = cursor.getString(0);
			}
		}
		cursor.close();
		db.close();
		return usernamelast;
	}
}
